home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: hpwin055.uksr!hpqmoea!neilg
- From: neilg@hpqt0319.sqf.hp.com (Neil Gall)
- Subject: Re: ugly constants and header files
- Sender: news@hpqmoea.sqf.hp.com (SQF News Admin)
- Message-ID: <DKvIKs.ACz@hpqmoea.sqf.hp.com>
- Date: Mon, 8 Jan 1996 17:43:40 GMT
- References: <k607w0zpkP3U088yn@merlin.magic.mb.ca>
- Nntp-Posting-Host: hpqt0319.sqf.hp.com
- Organization: Hewlett-Packard LTD, South Queensferry, Scotland
- X-Newsreader: TIN [version 1.1 PL8.8]
-
- David C. Wright wrote:
-
- > Create a single header file, of the form:
-
- [ stuff ]
-
- > #ifdef GLOBALS
-
- > int globalvar;
- > anystruct_t *ptr1;
- > enumvars_t evars;
-
- > #else
-
- > extern int globalvar;
- > extern anystruct_t *ptr1;
- > extern enumvars_t evars;
-
- > #endif
-
- [ stuff ]
-
- > #include this file in any modules that use the #defines/variables/prototypes
- > in question. In *one* of the modules, use;
-
- > #define GLOBALS
-
- NO NO NO NO NO!!! Aargh! This is *really* horrible. Firstly, you've
- got two declarations for the same variables but the compiler isn't aware
- of the fact - if you change one and not the other by mistake you'll spend
- ages looking for that wierd bug. In short it's a maintenance nightmare.
- Secondly, declaring variables in header files is generally considered to
- be pretty poor style. I've seen this done so many times, sometimes
- disguised with some macros to avoid the double declaration problem, like
- this:
-
- #ifdef GLOBALS
- #define variable
- #else
- #define variable extern
- #endif
-
- variable int x; etc...
-
- This is just as ugly, and doesn't get away from the fact that variables
- are defined in header files, which (I'll say this again) is a pretty poor
- coding style and can be error prone. What happens if you accidentally
- defined GLOBALS in another file ? You'll get linker problems.
-
- There is one simple, clean and effective way of doing this which will
- make your compiler complain if you do anything silly and which avoids
- all these problems:
-
- Declare all the variables in a header file, say vars.h
-
- extern int globalvar;
- extern anystruct_t *ptr1;
- ...etc
-
- And define them all in a source file, say vars.c, but remember to include
- vars.h. This way you still define the variable in two places, but the
- compiler will barf if there is a mismatch.
-
- #include "vars.h"
-
- int globalvar = 1;
- anystruct_t *ptr1 = NULL;
-
- What all this boils down to is that C is absolutely pathetic at handling
- multiple source files. In fact, since it relies on the preprocessor you
- could argue that C has no support at all for multiple source files.
- Since using variables across multiple files is not very well supported
- in the language, all sorts of wierd and wonderful schemes have been
- designed to do it. In my mind, what to think about when doing this is
- keeping the interface and the implementation of your modules separate.
- Keep the interface in .h files and the implementation in .c files. This
- fits neatly into C's limitations.
-
- What I want to see in the next standard is a better way of exporting
- module interfaces to other modules which can't be abused in the way that
- header files tend to be.
-
- --
- | Neil Gall, Hewlett-Packard Ltd, Queensferry Telecoms Operation, |
- | South Queensferry, Scotland +44 (0)131 331 7112 |
- | neilg@hpsqf.sqf.hp.com http:/www.tardis.ed.ac.uk/~chrism/neil/nb/ |
- | "That's what it'll be like in the future..." |
-